package com.pyound.utils;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * FastDFS--工具包
 */
public class FastDFSUtil {

    private static TrackerClient trackerClient;//tracker客户端对象
    private static TrackerServer trackerServer;//tracker服务

    /**
     * 加载配置信息
     */
    static {
        try {
            //查找classpath下的文件路径
            String filePath = new ClassPathResource("fdfs_client.conf").getPath();
            //加载Tracker链接信息
            ClientGlobal.init(filePath);
            //创建一个Tracker访问的客户端对象TrackerClient
            trackerClient = new TrackerClient();
            //通过TrackerClient访问TrackerServer服务，获取连接信息
            trackerServer = trackerClient.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取文件信息
     */
    public static FileInfo getFileInfo(String groupName, String remoteFIleName) {
        try {
            //通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
            StorageClient storageClient = new StorageClient(trackerServer, null);
            FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFIleName);
            return fileInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件上传
     */
    public static String[] upload(String fileExt, byte[] buff) {
        try {
            //通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
            StorageClient storageClient = new StorageClient(trackerServer, null);
            //文件上传
            String[] uploadFile = storageClient.upload_file(buff, fileExt, null);
            return uploadFile;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件下载
     */
    public static InputStream downloadFile(String groupName, String remoteFileName) {
        try {
            //通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
            StorageClient storageClient = new StorageClient(trackerServer, null);
            byte[] buffer = storageClient.download_file(groupName, remoteFileName);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
            return inputStream;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件删除
     *
     * @param groupName      文件组名
     * @param remoteFIleName 文件路径
     */
    public static void deleteFile(String groupName, String remoteFIleName) {
        try {
            //通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
            StorageClient storageClient = new StorageClient(trackerServer, null);
            storageClient.delete_file(groupName, remoteFIleName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        //通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        FileInfo fileInfo = storageClient.get_file_info("group1", "M00/00/00/wKjFgWCQ41KAZjMRAAjU_8u5L4c901.jpg");
        System.out.println(fileInfo);
    }


}
